package com.qf.controller;

import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.util.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class Demo1Controller {


    @Autowired
    private UserService userService;
    @GetMapping("/user/{id}")
    public R findUserById(@PathVariable int id){
        User user = userService.findUserById(id);
        if(user==null){
            return R.fail(5000,"查无此人");
        }
        return R.success(user);
    }
    @GetMapping("/user/list")
    public List<User> listuser(){
        List<User> all = userService.findAll();
        return all;
    }
    @GetMapping("/user/login")
    public R loginByUsernameAndPassword(String username,String password){
       User user=userService.loginByUsernameAndPassword(username,password);
        return R.success(user);
    }

    @PostMapping("/user/add")
    public R addUser(User user){
       int n= userService.addUser(user);
       if (n>0){
           return R.success();
       }
       return R.fail();
    }
    @GetMapping("/user/delete/{id}")
    public R deleteUser(@PathVariable int id){
        int m=userService.deleteUser(id);
        if(m>0){
            return R.success();
        }
        return R.fail(5000,"查无此人");
    }
    @GetMapping("/user/update")
    public R updateUser(User user){
        int l=userService.updateUser(user);
        if(l>0){
            return R.success();
        }
        return R.fail(5000,"更新失败");
    }

    @GetMapping("/user/map")
    public R findUserByMap(Map<String,Object> map){
        List<User> user=userService.findUserByMap(map);
        return R.success(user);
    }
}
